#include "../mqserver/mq_consumer.hpp"
#include <gtest/gtest.h>

bitmq::ConsumerManager::ptr cmp;
class ConsumerTest : public testing::Environment
{
    virtual void SetUp() override
    {
        cmp = std::make_shared<bitmq::ConsumerManager>();
        cmp->initQueueConsumer("q1");
    }

    virtual void TearDown() override
    {
        cmp->clear();
    }
};

void cb(const std::string &tag, const bitmq::BasicProperties *bp, const std::string &body)
{
    std::cout << tag << " 消费了消息：" << body << std::endl;
}

TEST(consumer_test, insert_test)
{
    cmp->create("c1", "q1", false, cb);
    cmp->create("c2", "q1", false, cb);
    cmp->create("c3", "q1", false, cb);

    ASSERT_EQ(cmp->exists("c1", "q1"), true);
    ASSERT_EQ(cmp->exists("c2", "q1"), true);
    ASSERT_EQ(cmp->exists("c3", "q1"), true);
}

TEST(consumer_test, remove_test)
{
    cmp->remove("c1", "q1");

    ASSERT_EQ(cmp->exists("c1", "q1"), false);
    ASSERT_EQ(cmp->exists("c2", "q1"), true);
    ASSERT_EQ(cmp->exists("c3", "q1"), true);
}

TEST(consumer_test, choose_test)
{
    bitmq::Consumer::ptr cp = cmp->choose("q1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->tag, std::string("c2"));

    cp = cmp->choose("q1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->tag, std::string("c3"));

    cp = cmp->choose("q1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->tag, std::string("c2"));
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    int t = RUN_ALL_TESTS();
    return 0;
}